package com.kimho.cost.controller;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.kimho.core.PageInfo;
import com.kimho.cost.domain.Cost;
import com.kimho.cost.service.CostService;
import com.kimho.inhouse.domain.Inhouse;
import com.kimho.user.domain.User;
import com.kimho.util.Util;
@Controller
@RequestMapping("/cost")
public class CostController {


	@Autowired
	private CostService costService;
	/**
	 * 登录验证
	 * @param request
	 * @param response
	 * @param user
	 * @return
	 */
	@RequestMapping(value="/items",method=RequestMethod.POST)
	public @ResponseBody Map<String,Object> getCostList(HttpServletRequest request,   
            HttpServletResponse response){
		Map<String,Object> responseMap = new HashMap<String,Object>();
		PageInfo pi = new PageInfo(request);
		List<Cost> costList = costService.getCostList(pi);
		responseMap.put("totalCount", costList.size());
		responseMap.put("rows", costList);
		return responseMap;
	}
	
	/*下账列表*/
	@RequestMapping(value="/sendbill_list",method=RequestMethod.POST)
	public @ResponseBody Map<String,Object> sendbill_list(HttpServletRequest request,   
            HttpServletResponse response){
		Map<String,Object> responseMap = new HashMap<String,Object>();
		Map<String, Object> map=new HashMap<String, Object>();
	//	PageInfo pi = new PageInfo(request);
		if((User)request.getSession().getAttribute("user")!=null && ((User)request.getSession().getAttribute("user")).getPost_id()!=1){//如果登陆用户的较色不是1(经理),就只查询登陆地点的数据
			map.put("login_location", request.getSession().getAttribute("login_localtion"));
		}
		List<Cost> sendbill_list = costService.sendbill_list(map);
		responseMap.put("totalCount", sendbill_list.size());
		responseMap.put("rows", sendbill_list);
		return responseMap;
	}
	@RequestMapping(value="/institems",method=RequestMethod.POST)
	public @ResponseBody Map<String,Object> getInstList(HttpServletRequest request,   
            HttpServletResponse response){
		Map<String,Object> responseMap = new HashMap<String,Object>();
		PageInfo pi = new PageInfo(request);
		List<Cost> instList = costService.getInstList(pi);
		responseMap.put("totalCount", instList.size());
		responseMap.put("rows", instList);
		return responseMap;
	}
	
	@RequestMapping(value="/isntitems_o",method=RequestMethod.POST)
	public @ResponseBody Map<String,Object> getInstListo(HttpServletRequest request,   
            HttpServletResponse response){
		Map<String,Object> responseMap = new HashMap<String,Object>();
		PageInfo pi = new PageInfo(request);
		List<Cost> instList = costService.getInstListo(pi);
		responseMap.put("totalCount", instList.size());
		responseMap.put("rows", instList);
		return responseMap;
	}
	@RequestMapping(value="/debt",method=RequestMethod.POST)
	public @ResponseBody Map<String,Object> getdebtList(HttpServletRequest request,   
            HttpServletResponse response){
		Map<String,Object> responseMap = new HashMap<String,Object>();
		PageInfo pi = new PageInfo(request);
		List<Cost> costList = costService.getdebtList(pi);
		responseMap.put("totalCount", costList.size());
		responseMap.put("rows", costList);
		return responseMap;
	}
	
	
	@RequestMapping(value="/manager",method=RequestMethod.POST)
	public @ResponseBody Map<String,Object> costmanager(HttpServletRequest request,   
            HttpServletResponse response){
		Map<String,Object> responseMap = new HashMap<String,Object>();
		DateFormat format= new SimpleDateFormat("yyyy-MM-dd");
		String id = request.getParameter("id");
		String object_id = request.getParameter("object_id");
		String cost_id = request.getParameter("cost_id");
		String cost_type = request.getParameter("cost_type");
		String t_traff_fee = request.getParameter("t_traff_fee");
		String t_mid_fee = request.getParameter("t_mid_fee");
		String t_saf_fee = request.getParameter("t_saf_fee");
		String t_inst_fee = request.getParameter("t_inst_fee");
		String t_oth_fee = request.getParameter("t_oth_fee");
		String settle = request.getParameter("settle");
		
		
		try {
			{
				Cost cost = new Cost();
				String newid = Util.getUUID();
				cost.setObject_id(object_id);
			    cost.setCost_id(cost_id);
			    cost.setCost_type(cost_type);
			    cost.setT_inst_fee(Double.parseDouble(t_inst_fee));
			    cost.setT_mid_fee(Double.parseDouble(t_mid_fee));
			    cost.setT_oth_fee(Double.parseDouble(t_oth_fee));
			    cost.setT_saf_fee(Double.parseDouble(t_saf_fee));
			    cost.setT_traff_fee(Double.parseDouble(t_traff_fee));
			    cost.setSettle(settle);
			    
			    
				Map<String,Object> param = new HashMap<String,Object>();
				costService.updateCost(cost, param);
				if( "1".equals(settle) && (cost.getT_inst_fee() > 0) )
				{
					costService.insertInst(cost,param);
				}
				responseMap.put("method", "Create");
				responseMap.put("success", "true");
				responseMap.put("info", "新增成功！");
			}
			return responseMap;
		}catch(Exception e) {
			e.printStackTrace();
			responseMap.put("info", e.getClass()+":"+e.getMessage());
			return responseMap;
		}
	}
	
	@RequestMapping(value = "/donebillok")
	public @ResponseBody Map<String, Object> donebillok(HttpServletRequest request,
			HttpServletResponse response) {
		Map<String, Object> responseMap = new HashMap<String, Object>();
		Map<String, Object> param = new HashMap<String, Object>();
		if(request.getSession().getAttribute("login_localtion")==null && request.getSession().getAttribute("login_localtion").equals("")){
			responseMap.put("success", false);
			responseMap.put("info", "新增失败,获取登陆地点失败,请重新登陆！");
			return responseMap;
		}
		long staff_code=((User)request.getSession().getAttribute("user")).getStaff_code();
		String location=(String)request.getSession().getAttribute("login_localtion");
		String bill_id=request.getParameter("bill_id");
		try {
			String id=Util.getUUID();
			param.put("bill_id", bill_id);
			param.put("staff_code",staff_code);
			param.put("location", location);
			param.put("id", id);
			this.costService.donebillok(param);
			responseMap.put("success", "true");
			responseMap.put("info", "下账成功！");
			return responseMap;
		} catch (Exception e) {
			e.printStackTrace();
			responseMap.put("success", "false");
			responseMap.put("info", "下账失败！  " + e.getClass() + ":"
					+ e.getMessage());
			return responseMap;
		}
	}
	@RequestMapping(value = "/sendbill_done")
	public @ResponseBody Map<String, Object> sendbill_done(HttpServletRequest request,
			HttpServletResponse response) {
		Map<String, Object> responseMap = new HashMap<String, Object>();
		Map<String, Object> param = new HashMap<String, Object>();
		if(request.getSession().getAttribute("login_localtion")==null && request.getSession().getAttribute("login_localtion").equals("")){
			responseMap.put("success", false);
			responseMap.put("info", "新增失败,获取登陆地点失败,请重新登陆！");
			return responseMap;
		}
		long staff_code=((User)request.getSession().getAttribute("user")).getStaff_code();
		String location=(String)request.getSession().getAttribute("login_localtion");
		String sum_fee=request.getParameter("bill_sum_fee");
		String bill_type=request.getParameter("bill_type");
		try {
			String ids = request.getParameter("ids");
			String[] id = null;
			if (ids != null && !ids .equals(" ") ) {
				id = ids.split(",");
			}
			System.out.println(id);
			String bill_id=Util.getUUID();
			param.put("bill_id", bill_id);
			param.put("staff_code",staff_code);
			param.put("location", location);
			param.put("bill_sum_fee",sum_fee);
			param.put("bill_type", bill_type);
			this.costService.bill_insert(param);
			this.costService.sendbill_done(id,bill_id);
			responseMap.put("success", "true");
			responseMap.put("info", "下账成功！");
			return responseMap;
		} catch (Exception e) {
			e.printStackTrace();
			responseMap.put("success", "false");
			responseMap.put("info", "下账失败！  " + e.getClass() + ":"
					+ e.getMessage());
			return responseMap;
		}
	}
		
	@RequestMapping(value="/donebill_list",method=RequestMethod.POST)
	public @ResponseBody Map<String,Object> donebill_list(HttpServletRequest request,   
            HttpServletResponse response){
		Map<String,Object> responseMap = new HashMap<String,Object>();
		PageInfo pi = new PageInfo(request);
		List<Cost> donebill_list = costService.donebill_list(pi);
		responseMap.put("totalCount", donebill_list.size());
		responseMap.put("rows", donebill_list);
		return responseMap;
	}
	
	@RequestMapping(value="/instmanager",method=RequestMethod.POST)
	public @ResponseBody Map<String,Object> costinstmanager(HttpServletRequest request,   
            HttpServletResponse response){
		Map<String,Object> responseMap = new HashMap<String,Object>();
		String pay_rat = request.getParameter("pay_rat");
		String sh_pay = request.getParameter("sh_pay");
		String re_pay = request.getParameter("re_pay");
		String deduct = request.getParameter("deduct");
		String settle = request.getParameter("settle");
		String inst_id=request.getParameter("inst_id");
		
		
		try {
			{
				Cost cost = new Cost();
				cost.setSh_pay(Double.parseDouble(sh_pay));
				cost.setRe_pay(Double.parseDouble(re_pay));
				cost.setPay_rat(Double.parseDouble(pay_rat));
				cost.setDeduct(Double.parseDouble(deduct));
				cost.setInst_id(inst_id);
			    cost.setSettle(settle);
			    
			    
				Map<String,Object> param = new HashMap<String,Object>();
				costService.updateInst(cost, param);
				responseMap.put("method", "Create");
				responseMap.put("success", "true");
				responseMap.put("info", "新增成功！");
			}
			return responseMap;
		}catch(Exception e) {
			e.printStackTrace();
			responseMap.put("info", e.getClass()+":"+e.getMessage());
			return responseMap;
		}
	}
	
}
